package com.wangdong.network.http;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.wangdong.network.utils.StringUtils;

import org.apache.http.Header;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 作者：wangdong on 2018/3/26 17:30
 * 邮箱：2100520956@qq.com
 */

public abstract class CMBaseSender {

        protected Context mCtx;
        protected AsyncHttpClient mClient;
        protected static HashMap<String, String> params;//store public params

    //初始化公共参数
    public static void init(){
        params = new HashMap<String, String>();

    }
    //添加网络访问参数
    public static void addParams(Object object){
        HashMap<String, String> hashMap = objToHash(object);
        params.putAll(hashMap);
    }

    //移除网络访问里面指定key值得参数
    public static void removeUid(){
        params.remove("uid");
    }

    public abstract <T> void get(String url, Class<?> cls, Object object, CMJsonCallback<T> callback);
    public abstract <T> void post(String url, Class<?> cls, Object object, CMJsonCallback<T> callback);
    public abstract <T> void postFile(Object object, CMJsonCallback<T> callback);
    public abstract void cancelAll();

    class ExtralCallback<T> extends CMJsonHandler<T> {

        private CMJsonCallback<T> callback;

        public ExtralCallback(Class<?> mTargetCls, String tag, CMJsonCallback<T> callback) {
            super(mTargetCls);
            this.callback =callback;
        }

        @Override
        public void onSuccessToSub(T t, Header[] headers) {
           if (null == callback){
               return;
           }
           try {
               callback.onSuccess(t);
           }catch(Throwable e){
               e.printStackTrace();
           }

        }

        @Override
        public void onErrorToSub(int statusCode, String msg) {
            if (null == callback){
                return;
            }
            try {
                callback.onError(statusCode,msg);
            }catch(Throwable e){
                e.printStackTrace();
            }

        }

        @Override
        public void onFailureToSub(int statusCode, Throwable throwable, Header[] headers) {
            if (null == callback){
                return;
            }
            try{
                callback.onFail(statusCode,throwable.getMessage());
            }catch(Throwable e){
                e.printStackTrace();
            }
        }
    }

    //把一个类属性变成键值对 的HashMap集合
    public static HashMap<String, String> objToHash(Object obj){

        LinkedHashMap<String, String> hashMap = new LinkedHashMap<String, String>();
        try {
            Class clazz = obj.getClass();//返回对应的当前正在运行时的类所对应的对象
            List<Class> clazzs = new ArrayList<Class>();
            do {
                clazzs.add(clazz);
                clazz = clazz.getSuperclass();//返回当前实体类的父类
            }while (!clazz.equals(Object.class));

            for (Class iClazz : clazzs){
                Field[] fields =iClazz.getDeclaredFields();//这些对象反映此 Class 对象所表示的类或接口所声明的所有字段 也就是属性
                for (Field field : fields){
                    String objVal =null;
                    field.setAccessible(true);//将此对象的 accessible 标志设置为指示的布尔值。作用去除私有权限
                    if (field.get(obj)!=null){// 返回指定对象上此 Field 表示的字段的值
                        objVal = String.valueOf(field.get(obj));
                        if (!StringUtils.isEmpty(objVal)){
                            hashMap.put(field.getName(),objVal);
                        }

                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hashMap;
    }

    //留一个方法，给公共参数添加参数
    public void addPublicParam(Object object) throws Exception {
        if (null == params || params.size() <= 0){
            throw new Exception("it has not been initialized,please init network firstly!");
        }
        params.putAll(objToHash(object));
    }




}
